//
//  TKMMulBrowserCell.m
//  TKPhotoSelectDemo
//
//  Created by liupm on 16/4/21.
//  Copyright © 2016年 liupm. All rights reserved.
//

#import "TKMMulBrowserCell.h"
@interface TKMMulBrowserCell()<UIScrollViewDelegate,UIGestureRecognizerDelegate>
@property(nonatomic,strong) UIScrollView *scrollView;
@property(nonatomic,strong) UIImageView *imageView;
@end
@implementation TKMMulBrowserCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self drawView];
    }
    return self;
}

-(void)drawView
{
    [self.scrollView addSubview:self.imageView];
    [self addSubview:self.scrollView];
    //添加单击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    [self addGestureRecognizer:tap];
    //添加双击手势
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
    tap2.delegate = self;
    tap2.numberOfTapsRequired = 2;
    [tap requireGestureRecognizerToFail: tap2];
    [self addGestureRecognizer:tap2];
}


/**
 *  @Author lpm, 16-04-20 22:04:35
 *
 *  @brief  赋值
 *
 *  @param model
 */
-(void)setModel:(TKIMPhotoModel *)model
{
    _model = model;
    [self.scrollView setZoomScale:1 animated:YES];
    self.imageView.image = nil;
    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        UIImage *image = _model.originImage;
        dispatch_async(dispatch_get_main_queue(), ^{
            CGFloat scale = image.size.width/image.size.height;
            //重新设置imageView的长宽
            CGFloat imgHeight = weakSelf.scrollView.frame.size.width/scale;
            
            if (imgHeight > weakSelf.scrollView.frame.size.height) {
                  weakSelf.imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
            }else{
                   weakSelf.imageView.frame = CGRectMake(0, (weakSelf.scrollView.frame.size.height- imgHeight)/2, weakSelf.scrollView.frame.size.width,imgHeight);
            }

            self.imageView.image = image;
        });
    });
}


/**
 *  @Author lpm, 16-04-20 14:04:27
 *
 *  @brief  隐藏
 */
-(void)hide
{
    [self routerEventWithName:TKIMImageBrowersHideNavBarEvent userInfo:nil];
}


/**
 *  @Author lpm, 16-04-20 16:04:30
 *
 *  @brief  双击放大
 *
 *  @param g
 */
- (void)doubleTap:(UITapGestureRecognizer *)g
{
    if (self.scrollView) {
        if (self.scrollView.zoomScale > 1)
        {
            [self.scrollView setZoomScale:1 animated:YES];
        }
        else
        {
            CGPoint touchPoint = [g locationInView:self.imageView];
            CGFloat newZoomScale = self.scrollView.maximumZoomScale;
            CGFloat xsize = self.frame.size.width / newZoomScale;
            CGFloat ysize = self.frame.size.height / newZoomScale;
            [self.scrollView zoomToRect:CGRectMake(touchPoint.x - xsize/2, touchPoint.y - ysize/2, xsize, ysize) animated:YES];
        }
    }
}


#pragma mark scrollview代理
/**
 *  @Author lpm, 16-04-20 16:04:24
 *
 *  @brief  返回缩放的是哪个ImageView
 *
 *  @param scrollView
 *
 *  @return
 */
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return self.imageView;
}

/**
 *  @Author lpm, 16-04-20 16:04:35
 *
 *  @brief  实现图片在缩放过程中居中
 *
 *  @param scrollView
 */
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;
    self.imageView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);
}



#pragma mark 属性
-(UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.contentSize = _scrollView.frame.size;
        _scrollView.delegate = self;
        //开启多点触摸
        _scrollView.multipleTouchEnabled = YES;
        //设置最小缩放比例
        _scrollView.minimumZoomScale = 1.0;
        //设置最大缩放比例
        _scrollView.maximumZoomScale = 2.0;
    }
    return _scrollView;
}


-(UIImageView *)imageView
{
    if (!_imageView)
    {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _imageView.backgroundColor = [UIColor clearColor];
    }
    return _imageView;
}


@end
